<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-row>
      <el-form-item label="用户/项目" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入用户/项目名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车型/样车	" prop="carTypeDetil">
        <el-input
          v-model="queryParams.carTypeDetil"
          placeholder="请输入车型/样车	"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="反馈时间">
        <el-date-picker
          v-model="daterangeProblemFeedbackTime"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="车型" prop="carType">
        <el-select v-model="queryParams.carType" placeholder="请选择车型" clearable size="small">
          <el-option
            v-for="dict in carTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      </el-row>
      <el-row>
      <el-form-item label="系统" prop="workSystem">
        <el-select v-model="queryParams.workSystem" placeholder="请选择系统" clearable size="small">
          <el-option
            v-for="dict in workSystemOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="FMEA" prop="isFmea">
        <el-select v-model="queryParams.isFmea" placeholder="请选择FMEA积累" clearable size="small">
          <el-option
            v-for="dict in isFmeaOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="发生阶段" prop="problemStage">
        <el-select v-model="queryParams.problemStage" placeholder="请选择问题发生阶段" clearable size="small">
          <el-option
            v-for="dict in problemStageOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="问题分类" prop="questionClassification">
        <el-select v-model="queryParams.questionClassification" placeholder="请选择问题分类" clearable size="small">
          <el-option
            v-for="dict in questionClassificationOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
		  plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['quality:quality:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
		  plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['quality:quality:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
		  plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quality:quality:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" icon="el-icon-upload2" size="mini" @click="handleImport" v-hasPermi="['quality:quality:import']">导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
		  plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['quality:quality:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qualityList" @selection-change="handleSelectionChange" border style="width: 100%"
    height="600">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id" align="center" prop="qualityId" /> -->
      <el-table-column label="用户/项目" align="center" prop="name" />
      <el-table-column label="车型/样车	" align="center" prop="carTypeDetil" />
      <el-table-column label="反馈时间" align="center" prop="problemFeedbackTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.problemFeedbackTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="车型" align="center" prop="carType" :formatter="carTypeFormat" />
      <el-table-column label="系统" align="center" prop="workSystem" :formatter="workSystemFormat" />
      <el-table-column label="组别" align="center" prop="groups" />
      <el-table-column label="问题描述" align="center" prop="problemDescription" />
      <el-table-column label="原因分析" align="center" prop="causeAnalysis" />
      <el-table-column label="问题分类" align="center" prop="questionClassification"  :formatter="questionClassificationFormat"/>
      <el-table-column label="永久解决措施	" align="center" prop="permanentSolutions" />
      <el-table-column label="FMEA" align="center" prop="isFmea" :formatter="isFmeaFormat" />
      <el-table-column label="发生阶段" align="center" prop="problemStage" :formatter="problemStageFormat" />
      <el-table-column label="合同号" align="center" prop="contractNo" />
      <!-- <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="部门名称" align="center" prop="deptName" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['quality:quality:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['quality:quality:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质量信息对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
          <el-form-item label="FMEA" prop="isFmea">
          <el-select v-model="form.isFmea" placeholder="请选择FMEA积累">
            <el-option
              v-for="dict in isFmeaOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="问题发生阶段" prop="problemStage">
          <el-select v-model="form.problemStage" placeholder="请选择问题发生阶段">
            <el-option
              v-for="dict in problemStageOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
        <el-form-item label="用户/项目" prop="name">
          <el-input v-model="form.name" placeholder="请输入用户/项目名称" />
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="车型/样车	" prop="carTypeDetil">
          <el-input v-model="form.carTypeDetil" placeholder="请输入车型/样车	" />
        </el-form-item>
        </el-col>
        </el-row>
        <el-row>
        <el-col :span="12">
        <el-form-item label="反馈时间" prop="problemFeedbackTime">
          <el-date-picker clearable size="small"
            v-model="form.problemFeedbackTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择问题反馈时间">
          </el-date-picker>
        </el-form-item>
        </el-col>
        <el-col :span="12">
        <el-form-item label="车型" prop="carType">
          <el-select v-model="form.carType" placeholder="请选择车型">
            <el-option
              v-for="dict in carTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
        <el-form-item label="系统" prop="workSystem">
          <el-select v-model="form.workSystem" placeholder="请选择系统">
            <el-option
              v-for="dict in workSystemOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
          </el-col>
          <el-col :span="8">
        <el-form-item label="所属组别" prop="groups">
              <el-select v-model="form.groups" multiple  filterable  placeholder="请选择" prop="groupType" style="width:100%">
                <el-option v-for="item in groupTypeOptions" :key="item.id" :label="item.name" :value="item.code" :disabled="item.states == 1"></el-option>
              </el-select>
        </el-form-item>
          </el-col>
           <el-col :span="8">
              <el-form-item label="问题分类" prop="questionClassification">
                <el-select v-model="form.questionClassification" placeholder="请选择问题分类">
                  <el-option
                    v-for="dict in questionClassificationOptions"
                    :key="dict.dictValue"
                    :label="dict.dictLabel"
                    :value="dict.dictValue"
                  ></el-option>
                </el-select>
              </el-form-item>
          </el-col>
        </el-row>
        
        <el-form-item label="问题描述" prop="problemDescription">
          <el-input v-model="form.problemDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="原因分析" prop="causeAnalysis">
          <el-input v-model="form.causeAnalysis" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="永久解决措施	" prop="permanentSolutions">
          <el-input v-model="form.permanentSolutions" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        
        <el-form-item label="合同号" prop="contractNo">
          <el-input v-model="form.contractNo" placeholder="请输入合同号" />
        </el-form-item>
        <!-- <el-form-item label="所属机构" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入所属机构" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
      <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body :close-on-click-modal="false">
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers" :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading" :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的数据
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQuality, getQuality, delQuality, addQuality, updateQuality, exportQuality,importTemplate } from "@/api/quality/quality";
import {groupList} from "@/api/work/process";
import { getToken } from "@/utils/auth";
export default {
  name: "Quality",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 质量信息表格数据
      qualityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //导入参数
      upload: {
        // 是否显示弹出层（导入）
        open: false,
        // 弹出层标题（导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/quality/quality/importData",
      },
      // 问题反馈时间时间范围
      daterangeProblemFeedbackTime: [],
      // 车型(0牵引 1自卸 2专用 3宽体)字典
      carTypeOptions: [],
      //组别
      groupTypeOptions: [],
      // 问题分类(0 设计不合理干涉调整;1 设计错误;2 资料错误)字典
      questionClassificationOptions: [],
      // 系统(0整车 1动传 2底盘 3车身 4车电)字典
      workSystemOptions: [],
      // FMEA积累(0 已积累 1本次积累)字典
      isFmeaOptions: [],
      // 问题发生阶段(0生产 1售后 2试验 3试制)字典
      problemStageOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        carTypeDetil: null,
        problemFeedbackTime: null,
        carType: null,
        workSystem: null,
        isFmea: null,
        problemStage: null,
        questionClassification: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "用户/项目名称不能为空", trigger: "blur" }
        ],
        carTypeDetil: [
          { required: true, message: "车型/样车	不能为空", trigger: "blur" }
        ],
        carType: [
          { required: true, message: "车型不能为空", trigger: "change" }
        ],
        workSystem: [
          { required: true, message: "系统不能为空", trigger: "change" }
        ],
        problemDescription: [
          { required: true, message: "问题描述不能为空", trigger: "blur" }
        ],
        causeAnalysis: [
          { required: true, message: "原因分析不能为空", trigger: "blur" }
        ],
        permanentSolutions: [
          { required: true, message: "有效的永久解决措施	不能为空", trigger: "blur" }
        ],
        problemStage: [
          { required: true, message: "问题发生阶段不能为空", trigger: "change" }
        ],
        groups: [
          { required: true, message: "所属组别不能为空", trigger: "change" }
        ],
        problemFeedbackTime: [
          { required: true, message: "反馈时间不能为空", trigger: "change" }
        ],
        
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("qa_car_type").then(response => {
      this.carTypeOptions = response.data;
    });
    this.getDicts("qa_system_type").then(response => {
      this.workSystemOptions = response.data;
    });
    this.getDicts("qa_fmea_type").then(response => {
      this.isFmeaOptions = response.data;
    });
    this.getDicts("qa_stage_type").then(response => {
      this.problemStageOptions = response.data;
    });
    this.getDicts("qa_question_classification").then(response => {
      this.questionClassificationOptions = response.data;
    });
  },
  methods: {
    /** 查询质量信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeProblemFeedbackTime && '' != this.daterangeProblemFeedbackTime) {
        this.queryParams.params["beginProblemFeedbackTime"] = this.daterangeProblemFeedbackTime[0];
        this.queryParams.params["endProblemFeedbackTime"] = this.daterangeProblemFeedbackTime[1];
      }
      listQuality(this.queryParams).then(response => {
        console.log(response,'-----------------')
        this.qualityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 车型(0牵引 1自卸 2专用 3宽体)字典翻译
    carTypeFormat(row, column) {
      return this.selectDictLabel(this.carTypeOptions, row.carType);
    },
    // 系统(0整车 1动传 2底盘 3车身 4车电)字典翻译
    workSystemFormat(row, column) {
      return this.selectDictLabel(this.workSystemOptions, row.workSystem);
    },
    // 组别字典
    handleGroupList(){
      groupList().then((response) => {
        this.groupTypeOptions = response.data;
      });
    },
    // FMEA积累(0 已积累 1本次积累)字典翻译
    isFmeaFormat(row, column) {
      return this.selectDictLabel(this.isFmeaOptions, row.isFmea);
    },
    // 问题发生阶段(0生产 1售后 2试验 3试制)字典翻译
    problemStageFormat(row, column) {
      return this.selectDictLabel(this.problemStageOptions, row.problemStage);
    },
    // 问题分类(0 设计不合理干涉调整;1 设计错误;2 资料错误)
    questionClassificationFormat(row, column) {
      return this.selectDictLabel(this.questionClassificationOptions, row.questionClassification);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        qualityId: null,
        name: null,
        carTypeDetil: null,
        problemFeedbackTime: null,
        carType: null,
        workSystem: null,
        groups:[],
        groupList: null,
        problemDescription: null,
        causeAnalysis: null,
        permanentSolutions: null,
        isFmea: null,
        problemStage: null,
        contractNo: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptName: null,
        questionClassification: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeProblemFeedbackTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.qualityId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    selectChange(val){
        this.$forceUpdate();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.handleGroupList();
      this.open = true;
      this.title = "添加质量信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
   
      this.reset();
      this.handleGroupList();
      // this.selectChange();
      const qualityId = row.qualityId || this.ids
      getQuality(qualityId).then(response => {
         console.log(response,'this.form----------')
        this.form = response.qaQualityInfo;
        this.form.groups = response.groupLists;
        
        this.open = true;
        this.title = "修改质量信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        this.form.groupList = this.form.groups;
        this.form.groups = this.form.groups.join(',');
      
       //console.log(this.form,'this.form----------')
        if (valid) {
          if (this.form.qualityId != null) {
            updateQuality(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addQuality(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qualityIds = row.qualityId || this.ids;
      this.$confirm('是否确认删除质量信息编号为"' + qualityIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delQuality(qualityIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "质量信息导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      importTemplate().then((response) => {
        this.download(response.msg);
      });
    },
     // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有质量信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportQuality(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
